/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.vijos;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/4/18
 */
//硬币游戏
public class P5_1_1554 {

    public static int[] test(int t, int[] in) {
        dataVerify(t, in);
        List<Circle> circles = new ArrayList<>();
        for (int i = 0; i < in.length; i++) {
            circles.add(new Circle(in[i]));
            circles.add(new Circle(0));
        }
        Circle.print(circles);
        for (int i = 0; i < circles.size(); i++) {
            Circle circle = circles.get(i);
            if (i == 0) {
                circle.setNext(circles.get(i + 1)).setPrevious(circles.get(circles.size() - 1));
            } else if (i == circles.size() - 1) {
                circle.setNext(circles.get(0)).setPrevious(circles.get(i - 1));
            } else {
                circle.setPrevious(circles.get(i - 1)).setNext(circles.get(i + 1));
            }
        }
        while (t-- > 0) {
            Circle first = circles.get(0);
            Circle c = circles.get(0);
            int pValue = (int) c.getPrevious().oldValue;
            int nValue = (int) c.getNext().oldValue;
            c.setValue(pValue != nValue ? 2 : (pValue == 0) ? 0 : 1);
            c = first.getNext();
            while (c != first) {
                pValue = (int) c.getPrevious().oldValue;
                nValue = (int) c.getNext().oldValue;
                c.setValue(pValue != nValue ? 2 : (pValue == 0) ? 0 : 1);
                c = c.getNext();
            }
            Circle.print(circles);
            for (Circle circle: circles) {
                circle.updateValue();
            }
        }
        int[] result = new int[in.length * 2];
        for (int i = 0; i < circles.size(); i++) {
            result[i] = (int) circles.get(i).getValue();
        }
        return result;
    }

    private static void dataVerify(int t, int[] in) {

    }

    // 循环列表
    static class Circle {
        private Object oldValue;
        private Object value;
        private Circle next;
        private Circle previous;

        public Circle(Object value) {
            this.value = value;
            this.oldValue = value;
        }

        public void updateValue() {
            this.oldValue = this.value;
        }

        public Object getValue() {
            return value;
        }

        public static void print(List<Circle> circles) {
            int[] result = new int[circles.size()];
            for (int i = 0; i < circles.size(); i++) {
                result[i] = (int) circles.get(i).getValue();
            }
            System.out.println("circles:");
            printArray(result);
        }

        public Circle setValue(Object value) {
            this.value = value;
            return this;
        }

        public Circle getNext() {
            return next;
        }

        public Circle setNext(Circle next) {
            this.next = next;
            return this;
        }

        public Circle getPrevious() {
            return previous;
        }

        public Circle setPrevious(Circle previous) {
            this.previous = previous;
            return this;
        }

        public Object getOldValue() {
            return oldValue;
        }

        public Circle setOldValue(Object oldValue) {
            this.oldValue = oldValue;
            return this;
        }
    }

    // 打印int数组
    public static void printArray(int[] values) {
        StringBuilder sb = new StringBuilder().append('[');
        for (int i = 0; i < values.length; i++) {
            if (i != values.length - 1) {
                sb.append(values[i]).append(", ");
            } else {
                sb.append(values[i]);
            }
        }
        sb.append(']');
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        printArray(test(5, new int[] {2, 2, 2, 1, 1, 1, 1, 1, 1, 2}));
    }
}
